package gdut.jmxywz.action;

import gdut.jmxywz.been.ClassDown;
import gdut.jmxywz.been.Major;
import gdut.jmxywz.been.Stclass;
import gdut.jmxywz.been.Student;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

public class StclassAction extends BaseAction {

	private Stclass stclass;
	
	private Major major;
	
	private List majorList;
	
	private int stclassId;

/*----------------分页----------------------*/
	private PageModel pageModel;
	
	private int currentPage=1;
	
	private int pageSize=5;
	
/*----------------检索----------------------*/
	private String searchSelect="";
	
	private String like="like";
	
	private String searchValue="";
	
	private String excelParameter="";
	
/*----------------Excel导入导出----------------------*/
	private File excelFile;
	
	private boolean checkbox;
	
/***********************geter和setter方法*****************************/	
	public Stclass getStclass() {
		return stclass;
	}

	public void setStclass(Stclass stclass) {
		this.stclass = stclass;
	}

	public Major getMajor() {
		return major;
	}

	public void setMajor(Major major) {
		this.major = major;
	}
	
	public List getMajorList() {
		return majorList;
	}

	public void setMajorList(List majorList) {
		this.majorList = majorList;
	}
	
	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue)throws UnsupportedEncodingException {
		this.searchValue = new String(searchValue.getBytes("ISO-8859-1"),"utf-8");
	}

	public String getExcelParameter() {
		return excelParameter;
	}

	public void setExcelParameter(String excelParameter) {
		this.excelParameter = excelParameter;
	}
	
	public int getStclassId() {
		return stclassId;
	}

	public void setStclassId(int stclassId) {
		this.stclassId = stclassId;
	}
	
	public File getExcelFile() {
		return excelFile;
	}

	public void setExcelFile(File excelFile) {
		this.excelFile = excelFile;
	}

	public boolean isCheckbox() {
		return checkbox;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	/*################################################################################*/
	public String PreAdd() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Major";
		majorList=this.getCommonService().findListByHql(hql, null);
		return "preAdd";
	}

	
	public String Add() throws Exception {
		// TODO Auto-generated method stub
		boolean bl=this.getCommonService().saveObject(stclass);
		if(!bl){
			return "error";
		}
		return "add";
	}

	
	public String Show() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Stclass";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "stclassShow.action", "");
		return "show";
	}


	public String Search() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Stclass";
		String searchParameter="";
		
		String searchValue1=searchValue;
		
		if (like.equals("like")) {
			hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
		} else {
			hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
		}
		
		String enSearchValue=java.net.URLEncoder.encode(searchValue1,"UTF-8");//URL编码转换
		excelParameter="?searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		searchParameter="&searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"stclassSearch.action",searchParameter);

		return "search";
	}

	
	public String Detail() throws Exception {
		// TODO Auto-generated method stub
		stclass=(Stclass)this.getCommonService().findByIntId(stclassId, "gdut.jmxywz.been.Stclass");
		if(stclass==null){
			return "error";
		}
		return "detail";
	}

	
	public String PreUpdate() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Major";
		majorList=this.getCommonService().findListByHql(hql, null);
		
		stclass=(Stclass)this.getCommonService().findByIntId(stclassId, "gdut.jmxywz.been.Stclass");
		if(stclass==null){
			return "error";
		}
		return "preUpdate";
	}

	
	public String Update() throws Exception {
		// TODO Auto-generated method stub
		boolean bl=this.getCommonService().updateObject(stclass);
		if(!bl){
			return "error";
		}
		return "update";
	}

	
	public String Delete() throws Exception {
		// TODO Auto-generated method stub
		/*删除班级记录之前，先删除关联学生*/
		String deleteList="select stu from Student stu where stu.stclass.stclassId="+stclassId;
		List deleteStudentList=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<deleteStudentList.size();i++){
			boolean d=this.getCommonService().deleteObject((Student)deleteStudentList.get(i));
			if(!d){
				return "error";
			}
		}
		
		/*删除班级记录之前，先删除课件下载关联*/
		String deleteList2="select cd from ClassDown cd where cd.stclass.stclassId="+stclassId;
		List deleteClassDownList=this.getCommonService().findListByHql(deleteList2, null);
		for(int i=0;i<deleteClassDownList.size();i++){
			boolean d=this.getCommonService().deleteObject((ClassDown)deleteClassDownList.get(i));
			if(!d){
				return "error";
			}
		}
		
		/*删除班级*/
		stclass=(Stclass)this.getCommonService().findByIntId(stclassId, "gdut.jmxywz.been.Stclass");
		if(stclass==null){
			return "error";
		}
		
		boolean bl=this.getCommonService().deleteObject(stclass);
		if(!bl){
			return "error";
		}
		return "delete";
	}

	public String ImportExcel() throws Exception {
		// TODO Auto-generated method stub
		int count=0;
		try{
		FileInputStream fis=new FileInputStream(excelFile);
		jxl.Workbook jwb=Workbook.getWorkbook(fis);
		int sheets=jwb.getNumberOfSheets();
		for(int i=0;i<sheets;i++){
			Sheet rs=jwb.getSheet(i);
			int columnNum=rs.getColumns();
			int rowNum=rs.getRows();
			if(rowNum>0&&columnNum!=4){
				System.out.println("--------------------导入格式不对----------------------");
				return "error";
			}
			Cell cell;
			for(int row=1;row<rowNum;row++){
				cell=rs.getCell(0, row);
				String hql="from Stclass stc where stc.stclassName='"+cell.getContents()+"'";
				List l=this.getCommonService().findListByHql(hql, null);
				
				if(l.size()==0){
					stclass=new Stclass();
					stclass.setStclassName(cell.getContents());
				}else{
					stclass=(Stclass)l.get(0);
				}
				
				cell=rs.getCell(1, row);
				stclass.setStclassMonitor(cell.getContents());
				
				cell=rs.getCell(2, row);
				String findMajorId="from Major m where m.majorName='"+cell.getContents()+"'";
				List lm=this.getCommonService().findListByHql(findMajorId, null);
				
				if(lm.size()==0){
					if(checkbox){
						major=new Major();
						major.setMajorName(cell.getContents());
						boolean bool=this.getCommonService().saveObject(major);
						String findAddMajorId="from Major m where m.majorName='"+cell.getContents()+"'";
						List lam=this.getCommonService().findListByHql(findAddMajorId, null);
						major=(Major)lam.get(0);		
					}else{
						continue;
					}
				}else{
					major=(Major)lm.get(0);
				}
				stclass.setMajor(major);
				
				cell=rs.getCell(3, row);
				stclass.setStclassDescript(cell.getContents());
				
				boolean bl;
				if(l.size()==0){
					bl=this.getCommonService().saveObject(stclass);
				}else{
					bl=this.getCommonService().updateObject(stclass);
				}
				if(!bl){
					System.out.println("------------------存入数据库出错-------------------");
				}
				count++;
			}
		}
		System.out.println("=================一共导入数据数：================="+count);
		jwb.close();
		fis.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return "importExcel";
	}
	
	public void ExportExcel() throws Exception {
		// TODO Auto-generated method stub
		try{
			String[] str=new String[]{"班级名称","班长","所属专业","班级描述"};
			List list;

			if(!searchSelect.equals("")){
				String hql="from Stclass";
				System.out.println("++++++++++++++++++++++++++++"+searchSelect+like+searchValue);
				String searchValue1=searchValue;
				
				if (like.equals("like")) {
					hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
				} else {
					hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
				}
				
				list=this.getCommonService().findListByHql(hql, null);
			}else{
				String hql="from Stclass";
				list=this.getCommonService().findListByHql(hql, null);
			
			}
			if(list==null)
				return;
			HttpServletResponse response=ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setContentType("APPLICATION/OCTET-STREAM ");
			response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("班级.xls","UTF-8"));
			WritableWorkbook wwb=Workbook.createWorkbook(response.getOutputStream());
			WritableSheet sheet=wwb.createSheet("sheet1", 0);
			sheet.setColumnView(0, 20);
			sheet.setColumnView(1, 20);
			sheet.setColumnView(2, 20);
			sheet.setColumnView(3, 20);
			
			Label label=null;
			for(int i=0;i<str.length;i++){
				label=new Label(i,0,str[i]);
				sheet.addCell(label);
			}
			for(int i=0;i<list.size();i++){
				stclass=(Stclass)list.get(i);
				
				label=new Label(0,i+1,stclass.getStclassName());
				sheet.addCell(label);
				label=new Label(1,i+1,stclass.getStclassMonitor());
				sheet.addCell(label);
				label=new Label(2,i+1,stclass.getMajor().getMajorName());
				sheet.addCell(label);
				label=new Label(3,i+1,stclass.getStclassDescript());
				sheet.addCell(label);
		
			}
			wwb.write();
			wwb.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
}
